package com.woniuxy.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.woniuxy.pojo.Manager;
import com.woniuxy.service.ManagerService;
import com.woniuxy.tools.MD5Tools;

public class UserRealm extends AuthorizingRealm{

	@Autowired
	private ManagerService managerService;
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "myShiro";
	}
	
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub
		
		String name=(String)token.getPrincipal();
		String password=String.valueOf((char[])token.getCredentials());
		ByteSource salt=ByteSource.Util.bytes(name);
		String newPassword=MD5Tools.getMD5String(salt, password);
		Manager manager=managerService.login(name, newPassword);
		
		
		AuthenticationInfo info=new SimpleAuthenticationInfo(manager, newPassword,salt,getName());
		return info;
	}

}
